
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using wicpowerTcpTool.Models;
using wicpowerTcpTool.Network;

class TestForwardGroups
{
    static void Main()
    {
        // 基础测试
        TestBasicForwardGroups();
        
        // 新增的边界测试
        TestEdgeCases();
        
        Console.WriteLine("所有测试完成，按任意键退出...");
        Console.ReadKey();
    }

    static void TestBasicForwardGroups()
    {
        Console.WriteLine("=== 基础转发组测试 ===");
        var group1 = new ForwardGroup(1, 8001, 8002, "测试组1");
        var group2 = new ForwardGroup(2, 8003, 8004, "测试组2");

        try
        {
            group1.Start();
            Console.WriteLine("测试组1 已启动 (8001→8002)");
            
            group2.Start();
            Console.WriteLine("测试组2 已启动 (8003→8004)");

            Console.WriteLine("基础测试运行中...按任意键继续");
            Console.ReadKey();
        }
        finally
        {
            group1.Stop();
            group2.Stop();
            Console.WriteLine("基础测试组已停止");
        }
    }

    static void TestEdgeCases()
    {
        Console.WriteLine("\n=== 边界条件测试 ===");
        
        // 测试1: 同一IP下相邻端口
        Console.WriteLine("测试1: 同一IP下相邻端口");
        var adjacentGroup1 = new ForwardGroup(3, 8010, 8011, "相邻端口组1");
        var adjacentGroup2 = new ForwardGroup(4, 8011, 8012, "相邻端口组2");
        
        try
        {
            adjacentGroup1.Start();
            Console.WriteLine("相邻端口组1 已启动 (8010→8011)");
            
            adjacentGroup2.Start();
            Console.WriteLine("相邻端口组2 已启动 (8011→8012)");
            
            Console.WriteLine("相邻端口测试通过！");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"相邻端口测试失败: {ex.Message}");
        }
        finally
        {
            adjacentGroup1.Stop();
            adjacentGroup2.Stop();
        }

        // 测试2: 端口冲突检测
        Console.WriteLine("\n测试2: 端口冲突检测");
        var conflictGroup = new ForwardGroup(5, 8020, 8021, "冲突测试组");
        try
        {
            conflictGroup.Start();
            Console.WriteLine("冲突测试组 已启动 (8020→8021)");
            
            // 尝试启动使用相同端口的组
            try
            {
                var conflictGroup2 = new ForwardGroup(6, 8020, 8022, "冲突组2");
                conflictGroup2.Start();
                Console.WriteLine("错误！端口冲突检测未生效");
                conflictGroup2.Stop();
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine($"端口冲突检测成功: {ex.Message}");
            }
        }
        finally
        {
            conflictGroup.Stop();
        }

        // 测试3: 并发启动
        Console.WriteLine("\n测试3: 并发启动多个转发组");
        var tasks = new Task[3];
        for (int i = 0; i < 3; i++)
        {
            int groupId = 10 + i;
            tasks[i] = Task.Run(() => {
                var group = new ForwardGroup(groupId, 8030 + i, 8040 + i, $"并发组{i}");
                try
                {
                    group.Start();
                    Console.WriteLine($"并发组{i} 已启动 ({8030 + i}→{8040 + i})");
                    Thread.Sleep(1000);
                }
                finally
                {
                    group.Stop();
                }
            });
        }
        Task.WaitAll(tasks);
        Console.WriteLine("并发启动测试完成");
    }
}
